﻿#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
#include<math.h>
#include<assert.h>
//法一：函数指针
void put()
{
	 printf("*************************\n");
	 printf("******1:add  2:sub*******\n");
	 printf("******3:mul  4:div*******\n");
	 printf("*************************\n");
	 printf("请选择：");
}
int add(int a, int b)
{
	return a + b;
}
int sub(int a, int b)
{
	return a - b;
}

int mul(int a, int b)
{
	return a * b;
}

int div(int a, int b)
{
	return a / b;
}
void calc(int(*pf)(int,int))
{
	int x = 0, y = 0;
	int ret = 0;
	printf("请输入两个操作数：");
	scanf("%d%d", &x, &y);
	ret = pf(x, y);
	printf("%d\n", ret);
}
int main()
{
	int input = 0;
	do
	{
		put();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			calc(add);
			break;
		case 2:
			calc(sub);
			break;
		case 3:
			calc(mul);
			break;
		case 4:
			calc(div);
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);
	return 0;
}



//法二：函数指针数组
void put()
{
	printf("*************************\n");
	printf("******1:add  2:sub*******\n");
	printf("******3:mul  4:div*******\n");
	printf("*************************\n");
	printf("请选择：");
}
int add(int a, int b)
{
	return a + b;
}
int sub(int a, int b)
{
	return a - b;
}

int mul(int a, int b)
{
	return a * b;
}

int div(int a, int b)
{
	return a / b;
}
int main()
{
	int input = 0;
	int x = 0, y = 0;
	int ret = 0;
	int (*arr[5])(int, int) = { 0,add,sub,mul,div };
	do
	{
		put();
		scanf("%d", &input);
		if (input == 0)
		{
			printf("退出计算器\n");
		}
		else if (input >= 1 && input <= 4)
		{
			printf("请输入两个操作数：");
			scanf("%d%d", &x, &y);
			ret = arr[input](x, y);
			printf("%d\n", ret);
		}
		else
		{
			printf("选择错误\n");
		}
	} while (input);
	return 0;
}


